package com.ycMc.controller;

import com.ycMc.bean.request.InStockRequest;
import com.ycMc.bean.request.MoneyRecordRequest;
import com.ycMc.bean.response.ListResponse;
import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.bean.response.PageBeanResponse;
import com.ycMc.common.bean.PageBean;
import com.ycMc.common.bean.Request;
import com.ycMc.common.bean.Response;
import com.ycMc.constant.ExcelPoiUtil;
import com.ycMc.domain.MoneyRecord;
import com.ycMc.domain.Order;
import com.ycMc.domain.User;
import com.ycMc.service.MoneyRecordService;
import com.ycMc.service.OrderService;
import com.ycMc.utils.DateUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by libo2 on 2017/8/23.
 */
@RequestMapping("api/moneyRecord")
@Controller
public class MoneyRecordController {


    @Autowired
    private MoneyRecordService moneyRecordService;

    @Autowired
    private OrderService orderService;


    @RequestMapping("insert")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:56
    *@Request :[request, httpServletRequest]
    *@Response:com.ycMc.common.bean.Response
     * 添加接口
    **/
    public Response insert(MoneyRecordRequest request,HttpServletRequest httpServletRequest){
        Response response = new Response();
        try{
            MoneyRecord moneyRecord = new MoneyRecord();

            BeanUtils.copyProperties(request,moneyRecord);
            User user =  (User)httpServletRequest.getSession().getAttribute("user");
            moneyRecordService.insert(moneyRecord,user.getUserId());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("update")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:56
    *@Request :[request]
    *@Response:com.ycMc.common.bean.Response
     * 修改接口
    **/
    public Response update(MoneyRecordRequest request){
        Response response = new Response();
        try{
            MoneyRecord moneyRecord = new MoneyRecord();

            BeanUtils.copyProperties(request,moneyRecord);

            moneyRecordService.update(moneyRecord);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("delete")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:57
    *@Request :[request]
    *@Response:com.ycMc.common.bean.Response
     * 通过moneyRecordId 删除
    **/
    public Response delete(MoneyRecordRequest request){
        Response response = new Response();
        try{
    moneyRecordService.delete(request.getMoneyRecordId());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }

        @RequestMapping("selectOne")
    @ResponseBody
        /**
        *Create by libo2
        *Time:2017/9/1 on 1:57
        *@Request :[request]
        *@Response:com.ycMc.bean.response.ObjectResponse
         * 通过moneyRecordId查一
        **/
    public ObjectResponse selectOne(MoneyRecordRequest request){
        ObjectResponse response = new ObjectResponse();
        try{
            response.setObject(moneyRecordService.selectOne(request.getMoneyRecordId()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("select")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:57
    *@Request :[request]
    *@Response:com.ycMc.bean.response.ListResponse
     * @ParamuserId
     @ParamtradeType
     @ParamoperatorId
     @ParampayMethod
     @ParampayId
     @ParamorderId
     @ParammoneyRecordIdmoneyRecordId查询
    **/
    public ListResponse select(MoneyRecordRequest request){
        ListResponse response = new ListResponse();
        try{
        response.setCommonList(moneyRecordService.select(request));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }


    @RequestMapping("selectByUserId")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:58
    *@Request :[request, httpServletRequest]
    *@Response:com.ycMc.bean.response.ListResponse
     * 通过userId查询
    **/
    public ListResponse selectByUserId(Request request,HttpServletRequest httpServletRequest){
        ListResponse response = new ListResponse();
        try{
            User user =  (User)httpServletRequest.getSession().getAttribute("user");
response.setCommonList(moneyRecordService.selectByUserId(user.getUserId()));
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    /**
    *Create by libo2
    *Time:2017/9/1 on 1:58
    *@Request :[request]
    *@Response:com.ycMc.bean.response.ListResponse
     * 查询所有
    **/
    public ListResponse selectAll(MoneyRecordRequest request){
        ListResponse response = new ListResponse();
        try{
        response.setCommonList(moneyRecordService.selectAll());
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("SUCCESS");
        }catch (Exception e){
            response.setMsg(e.getMessage());
            response.setStatusCode(Response.SUCCESS);
            e.printStackTrace();
        }
        return  response;
    }


    @RequestMapping("query")
    @ResponseBody
    public PageBeanResponse query(MoneyRecordRequest request){
        PageBeanResponse response = new PageBeanResponse();

        response.setPageBean(moneyRecordService.query(request));
        response.setStatusCode(Response.SUCCESS);
        response.setMsg("sucess");
        return  response;
    }

    @RequestMapping("downloadMoneyRecordByQueryCondition")
    @ResponseBody
    public Response downloadMoneyRecordByQueryCondition(MoneyRecordRequest request){
        Response  response = new Response();

        PageBean  pageBean= moneyRecordService.query(request);


        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("财务信息");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell(0);

        cell.setCellValue("序号");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("财务单编号");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("用户编号");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("金额");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("转账类型");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("备注信息");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("操作者");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("创建时间");
        cell.setCellStyle(style);
        cell = row.createCell(8);
        cell.setCellValue("支付方式");
        cell.setCellStyle(style);
        cell = row.createCell(9);
        cell.setCellValue("支付编号");
        cell.setCellStyle(style);
        cell = row.createCell(10);
        cell.setCellValue("订单编号");
        cell.setCellStyle(style);
        int i =0;
        List<MoneyRecord> moneyRecords = pageBean.getList();

        for(MoneyRecord moneyRecord:moneyRecords){
            row = sheet.createRow((int) i + 1);
            row.createCell(0).setCellValue(i);
            row.createCell(1).setCellValue(moneyRecord.getMoneyRecordId());
            row.createCell(2).setCellValue(moneyRecord.getUserId());
            row.createCell(3).setCellValue(moneyRecord.getMoney());
            row.createCell(4).setCellValue(moneyRecord.getTradeType());
            row.createCell(5).setCellValue(moneyRecord.getRemark());
            row.createCell(6).setCellValue(moneyRecord.getOperatorId());
            row.createCell(7).setCellValue(DateUtil.dateFormat3(moneyRecord.getCreateTime()));
            row.createCell(8).setCellValue(moneyRecord.getPayMethod());
            row.createCell(9).setCellValue(moneyRecord.getPayId());
            row.createCell(10).setCellValue(moneyRecord.getOrderId());
            i++;
        }
        try
        {
            String fileName = "MoneyRecord"+DateUtil.dateFormat2(System.currentTimeMillis())+".xls";
            String fileNameUrl = ExcelPoiUtil.Linux_url+fileName;
            FileOutputStream fout = new FileOutputStream(fileNameUrl);
            wb.write(fout);
            fout.close();
            response.setMsg("excelData/" + fileName);
            response.setStatusCode(Response.SUCCESS);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return response;
    }



}
